<template>
    <div>
        <search-component who="account" @getSearchInit="getSearchInit"></search-component>
        <div class="commonStyle nyContent">
            <div class="table-operator">
                <a-button type="primary" @click="$refs.addEdit.showAddModal()">添加</a-button>
            </div>
            <a-table :columns="columns"
                     :dataSource="loadData"
                     :rowKey="record => record.id"
                     :pagination="false">
            <span slot="index" slot-scope="text,record,index">
                {{(queryParam.currentPage-1)*queryParam.pageSize+index+1}}
            </span>
                <span slot="roleId" slot-scope="text">
                {{text==='1'?'管理员':(text==='2')?'操作员':'审计人员'}}
            </span>
                <span slot="operation" slot-scope="text" class="table-operation">
                <a @click="$refs.addEdit.showEdit(text)" v-if="text.protective!=='1'">编辑</a>
                <a @click="delUser(text)" v-if="text.protective!=='1' && text.userName != sessionUserName" class="adel">删除</a>
                     <a @click="resetRetryCount(text.userName)"
                        v-if="text.errCount >=6">用户解锁</a>
            </span>
            </a-table>
            <Page :totalCount="queryParam.totalCount" :currentPage="queryParam.currentPage" ref="page"
                  :pageSize="queryParam.pageSize"
                  @changePage="changePage" @changePageSize="changePageSize" />
            <add-edit ref="addEdit" @init="init" />
        </div>
    </div>
</template>

<script>
    import Page from 'module/pagination'
    import addEdit from './addEdit'
    import searchComponent from 'module/searchComponent'

    export default {
        name: 'account',
        components: {
            Page,
            addEdit,
            searchComponent
        },
        data () {
            return {
                sessionUserName:'',
                // 查询参数
                queryParam: {
                    pageSize: 10,
                    currentPage: 1,
                    searchKey: '',
                    totalCount: 0
                },
                loadData: [],
                columns: [
                    {title: '序号', key: 'index', width: '100px', align: 'center', scopedSlots: {customRender: 'index'}},
                    {title: '登录名', dataIndex: 'userName', key: 'userName', width: '20%'},
                    {title: '用户类型', dataIndex: 'roleId', key: 'roleId', scopedSlots: {customRender: 'roleId'}},
                    {title: '备注', dataIndex: 'remark', key: 'remark', scopedSlots: {customRender: 'remark'}},
                    {title: '更新时间', dataIndex: 'createTime', key: 'createTime'},
                    {title: '操作', key: 'operation', scopedSlots: {customRender: 'operation'}}
                ]
            }
        },
        methods: {
            /**
             * Function    : delUser
             * Description : 确定删除
             * Input       : 产品id
             * Return      : none
             */
            delUser (text) {
               let _this = this;
              _this.$confirm({
                title: `确定要删除 ${text.userName}吗?`,
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                  _this.$post("/sysuser/batchDelSysUsers", {ids: text.id}).then(res => {
                    if (res.code === 0) {
                      if(_this.loadData.length === 1 && _this.queryParam.currentPage>1){
                        _this.queryParam.currentPage = _this.queryParam.currentPage-1;
                      }
                      _this.init();
                      _this.$message.success(`删除 ${text.userName}成功！`);
                    } else {
                        _this.$message.error(res.message)
                    }
                  })
                }
              })
            },

            /**
             * Function    : resetRetryCount
             * Description : 用户解锁
             * Input       : userName 用户名
             * Return      : none
             */
            resetRetryCount ( userName ) {
                this.$confirm ({
                    title: `确定解锁${userName}用户吗?`,
                    okText: '确定',
                    cancelText: '取消',
                    onOk: () => {
                        let _this =this
                        _this.$post("/sysuser/resetRetryCount", {userName: userName}).then(res => {
                            if (res.code === 0) {
                                _this.$message.success (res.message)
                                _this.init ()
                            } else {
                                _this.$message.error(res.message)
                            }
                        })
                    }
                })
            },

            /**
             * Function    : init
             * Description : 初始化获取数据
             * Input       : none
             * Return      : none
             */
            init () {
                this.$get('/sysuser/findSysUser', Object.assign(this.queryParam)).then(res => {
                    if (res.code === 0) {
                        this.loadData = res.data.rows
                        this.queryParam.totalCount = res.data.total
                    } else {
                        this.$message.error(res.message)
                    }
                }).catch(res => {
                    this.$message.error(res.message)
                })
                this.sessionUserName = sessionStorage.getItem('user');
            },

            /**
             * Function    : init
             * Description : 获取搜索列表
             * Input       : none
             * Return      : none
             */
            getSearchInit (opt) {
                this.queryParam.searchKey = opt;
                this.queryParam.currentPage = 1;
                this.init()
            },

            /*切换页面*/
            changePage (page) {
                this.queryParam.currentPage = page;
                this.init()
            },
            changePageSize ( vm ) {
                this.queryParam.pageSize = vm
                this.queryParam.currentPage = 1
                this.init ()
            },
        },
        created () {
            this.init()
        }
    }
</script>
